//
//  YSPhotosView.m
//  Yueshi
//
//  Created by Northwan on 15/9/25.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSPhotosView.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "YSProgressImageView.h"


@interface YSPhotosView()<UIActionSheetDelegate>
@property (nonatomic, strong) NSMutableArray *photos;
@end

@implementation YSPhotosView

-(instancetype)init{
    if(self = [super init]){
        _photos = [NSMutableArray array];
        _photosView = [NSMutableArray array];
        self.size = CGSizeMake(SCREENW, photoW);
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    for (int i = 0; i<_photosView.count; i++) {
        YSProgressImageView *photoView =  _photosView[i];
//        photoView.x = i*photoW+YSPublicXInset;
        if(i == 0 ){
            photoView.x = YSPublicXInset;
        }else{
            photoView.x = CGRectGetMaxX(((YSProgressImageView *)_photosView[i-1]).frame)+YSPublicXInset;
        }
        [photoView setTag:i];
        [photoView setSize:CGSizeMake(photoW, photoW)];
    }
}

-(void)tapImage:(UITapGestureRecognizer *)sender{
    if(_isShowProgress){//删除
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:nil
                                      delegate:self
                                      cancelButtonTitle:@"取消"
                                      destructiveButtonTitle:nil
                                      otherButtonTitles:@"删除",nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [actionSheet setTag:sender.view.tag];
        [actionSheet showInView:theApp.window];
    }else{
        NSInteger count =  _photosView.count;
        // 1.封装图片数据
        NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
        for (int i = 0; i<count; i++) {
            MJPhoto *photo = [[MJPhoto alloc] init];
            photo.url = [NSURL URLWithString:_photos[i]]; // 图片路径
            photo.srcImageView = self.photosView[i]; // 来源于哪个UIImageView
            [photos addObject:photo];
        }
        
        // 2.显示相册
        MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
        browser.currentPhotoIndex = sender.view.tag; // 弹出相册时显示的第一张图片是？
        browser.photos = photos; // 设置所有的图片
        [browser show];

    }
}

-(void)addPhoto:(UIImage *)photo{
    if(_photos.count <= 4){//最多四张
        [_photos addObject:photo];
        [self addImage:_photos.count];
    }
}

-(void)addImage:(NSInteger)tag{
    YSProgressImageView *photoView = [YSProgressImageView new];
    [photoView setWhileShowIndicator:_isShowProgress];
    photoView.userInteractionEnabled = YES;
    photoView.layer.cornerRadius = 5;
    photoView.layer.masksToBounds = YES;
    photoView.contentMode = UIViewContentModeScaleAspectFill;
    if(_isShowProgress){
        [photoView setImage:_photos[tag]];
        /*
        //自动上传
        if(!photoView.isUpload){
            [photoView uploadImg:_photos[tag]];
        }else{
            [photoView setImage:_photos[tag]];
        }*/
    }else{
        NSString *url = [_photos[tag] stringByAppendingString:@"!160"];//取小图
        [photoView setImageURLStr:url];
    }
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];
    [photoView addGestureRecognizer:tap];
    [self addSubview:photoView];
    [_photosView addObject:photoView];
}

-(void)addPhotos:(NSArray *)photos{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.photosView removeAllObjects];
    [_photos removeAllObjects];
    [_photos addObjectsFromArray:photos];
  
    for (int i = 0; i<photos.count; i++) {
        [self addImage:i];
    }
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {//删除
        //row:当前 cell的row ， section:当前的图片tag
        NSIndexPath *deletePath =  [NSIndexPath indexPathForRow:self.tag inSection:actionSheet.tag];
        [[NSNotificationCenter defaultCenter]postNotificationName:kDeleteOrderReviewsSelectPictureNortif object:deletePath];
    }else if(buttonIndex == 1) {//取消
        NSLog(@"取消");
    }
    [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet{
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
    
}

@end
